using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Level : MonoBehaviour
{
    private List<Item> list = new List<Item>();
    public GameObject prefab;
    public Sprite sprite0;
    public Sprite sprite1;
    public Sprite sprite2;
    public Sprite sprite3;
    private Sprite sprite;

    [SerializeField] private SceneField MenuScenes;//菜单场景
    [SerializeField] private SceneField GameScenes;//游戏场景

    void Start()
    {
        Time.timeScale = 1;
        if(List.Instance != null) list = List.Instance.levelList.list;
        if(list == null) return;
         for (var i = 0; i < list.Count; i++)
        {
            GameObject go = Instantiate(prefab, transform.position, Quaternion.identity);
            go.transform.SetParent(transform, false);
            go.transform.Find("文本").GetComponent<Text>().text = (i + 1).ToString();
            if (list[i].num == -1)
            {
                go.transform.Find("锁").gameObject.SetActive(true);
            }
            else
            {
                if(list[i].num == 0) sprite = sprite0;
                if(list[i].num == 1) sprite = sprite1;
                if(list[i].num == 2) sprite = sprite2;
                if(list[i].num == 3) sprite = sprite3;
                go.transform.Find("星").GetComponent<Image>().sprite = sprite;
                int index = i;
                go.transform.GetComponent<Button>().onClick.AddListener(() => LoadGameScenes(index));
            }
        }
    }

    //加载菜单场景
    public void LoadMenuScenes()
    {
        AudioManager.Instance.PlaySFX("点击");
        SceneManager.LoadScene(MenuScenes);
    }

    //加载游戏场景
    public void LoadGameScenes(int index)
    {
        AudioManager.Instance.PlaySFX("点击");
        List.Instance.index = index;
        SceneManager.LoadScene(GameScenes);
    }
}
